<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<style type="text/css">
			* {
				margin: 0;
				padding: 0;
			}
			
			div {
				margin: 50px;
			}
			
			.div1 {
				width: 300px;
				background: gray;
			}
			
			.div2 {
				background: yellow;
			}
			
			.div3 {
				background: green;
				height: 100px;
				width: 100px;
			}
		</style>
	</head>

	<body>
		<div class="div1">
			div1
			<div class="div2">
				div2
				<div class="div3" id="first">
					first
				</div>
			</div>
		</div>

		<div class="div1">
			div1
			<div class="div2" style="position: relative;">
				div2 - position: relative
				<div class="div3" id="second">
					second
				</div>
			</div>
		</div>
	</body>

</html>

<script type="text/javascript">
	// 获取#first到页面左边界的距离(父节点没有加定位)
	var dis = document.querySelector('#first').offsetLeft;
	console.log('#fist到左边界的距离', dis);
	
	// 获取#second到左边界的距离:#second到它的offsetParent(.div2)的距离 + .div2到它的offsetParent(body)的距离
	var d1 = document.querySelector('#second').offsetLeft;
	console.log(d1);
	var d2 = document.querySelector('.div2').offsetLeft;
	var dis2 = d1 + d2;
	console.log('#second到左边界的距离', dis2);
	
</script>